//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Model.Core;
using Eas.WebCrawler.Interfaces;

namespace Eas.WebCrawler.Model.Actions {
    internal class PendingGetRequestAction : GetRequestAction {
        
        public override ProcessingAction EndAction(WorkerContext context, Eas.WebCrawler.Model.Core.BaseProcessingState state, IAsyncResult result) {
            ProcessingAction resultAction = base.EndAction(context, state, result);
            if (resultAction.ActionId != this.ActionId) return resultAction;

            if (context.StateTracker.GetCountOfWorkersInAction(ActionId.PendingGetRequest) + context.StateTracker.GetCountOfWorkersInAction(ActionId.Terminal) == context.WebCrawlerContext.WorkerCount) {
                context.EventReporter.ReportTrace(TraceLevel.Detailed, state, "No requests, and all workers are in the PendingGetRequest state, terminating.");
                return context.ActionFactory.CreateTerminalAction(state, context);
            }
            else {
                context.EventReporter.ReportTrace(TraceLevel.Detailed, state, "No requests but there are still open workers, waiting.");
                return this;
            }
        }

        public override ActionId ActionId {
            get { return ActionId.PendingGetRequest; }
        }

    }
}
